package com.lsf.modules.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.lsf.common.api.vo.Result;
import com.lsf.modules.system.entity.Role;
import com.lsf.modules.system.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author Alexander
 */
@RestController
@Slf4j
@RequestMapping("/sys/role")
public class RoleController {

    @Resource
    private RoleService roleService;

    /**
     * 添加角色
     *
     * @param role 角色
     * @return {@link Result}<{@link JSONObject}>
     */
    @PostMapping("/add")
    public Result<JSONObject> addRole(@RequestBody Role role) {
        Result<JSONObject> result = new Result<>();
        try {
            role.setCreateTime(new Date());
            roleService.save(role);
            result.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("添加失败");
        }
        return result;
    }
}
